/**  

* @Title: LoginAction.java 

* @Package com.gxbadge.action 

* @Description: TODO(用一句话描述该文件做什么) 

* @author CST

* @version V1.0  

*/ 

package com.gxbadge.action;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.gxbadge.action.base.BaseAction;
import com.gxbadge.model.Image;
import com.gxbadge.model.ImageInfo;
import com.gxbadge.model.Language;
import com.gxbadge.model.Navigation;
import com.gxbadge.util.DateTimeUtil;
import com.gxbadge.vo.ImageInfoVo;
import com.opensymphony.xwork2.ActionContext;

public class ImageAction extends BaseAction {
	
	
	private Image image;
	private List<Image> imageList;
	public Image getImage() {
		return image;
	}
	public void setImage(Image image) {
		this.image = image;
	}
	public List<Image> getImageList() {
		return imageList;
	}
	public void setImageList(List<Image> imageList) {
		this.imageList = imageList;
	}




	private Navigation nav;
	private List<Navigation> navList;
	
	public Navigation getNav() {
		return nav;
	}

	public void setNav(Navigation nav) {
		this.nav = nav;
	}

	public List<Navigation> getNavList() {
		return navList;
	}

	public void setNavList(List<Navigation> navList) {
		this.navList = navList;
	}
	private String[] parentId;
	public String[] getParentId() {
		return parentId;
	}
	public void setParentId(String[] parentId) {
		this.parentId = parentId;
	}

	public String insert_image() throws Exception{
		
		if(this.getNavList()!=null){
			this.setNavList(null);
		}
		if(this.getNav()!=null&&this.getNav().getNavId()!=null){
			
			Navigation nav = adminManager.findNavByNavId(this.getNav().getNavId());
			if(nav!=null){
				
				this.setNav(nav);
				List<Navigation> navList = adminManager.findNavListByNavPid(nav.getNavId());
				if(navList!=null&&navList.size()>=1){
					this.setNavList(navList);
				}	
			}
			
		}
		return "image_insert_success";
	}
	private File imageInput;
	private String  imageInputFileName;
	private String savePath;
	private String fileExt;
	public File getImageInput() {
		return imageInput;
	}

	public void setImageInput(File imageInput) {
		this.imageInput = imageInput;
	}

	public String getImageInputFileName() {
		return imageInputFileName;
	}

	public void setImageInputFileName(String imageInputFileName) {
		this.imageInputFileName = imageInputFileName;
	}

	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath("\\upload");
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String getFileExt() {
		return fileExt;
	}

	public void setFileExt(String fileExt) {
		this.fileExt = fileExt;
	}
	private static int[] imageIdList = new int[100];
	private static int count = 0;
	public String uploadify_image() throws Exception {
		
		
		String newFileName = "";// 新文件名
		String nowTime =null;
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		nowTime = new SimpleDateFormat("yyyymmddHHmmss").format(new Date());// 当前时间
		
		String savePath = this.getSavePath();
		
		String fileExt = this.getImageInputFileName().substring(this.getImageInputFileName().lastIndexOf(".") + 1).toLowerCase();
		
		newFileName = nowTime+"_"+new Random().nextInt(1000)+"."+fileExt;
		
		this.getImageInput().renameTo(new File(savePath + "\\" + newFileName));
		Image image = new Image();
		image.setImageSaveName(newFileName);
		image.setImageSavePath("upload/" + newFileName);
		image.setUploadDate(DateTimeUtil.getNowTime());
		image.setDeleteFlag(false);
		image.setHotFlag(false);
		adminManager.add_image(image);
		
		imageIdList[count] = image.getImageId().intValue();
		
		count++;
		response.getWriter().print("<font color='red'>文件上传成功</font>");
		
		return null;
	}
	
	private ImageInfo imageInfo;

	public ImageInfo getImageInfo() {
		return imageInfo;
	}

	public void setImageInfo(ImageInfo imageInfo) {
		this.imageInfo = imageInfo;
	}
	
	
	
	public String add_image() throws Exception {
		
		if(this.getParentId()!=null){
			
			int navId = Integer.parseInt(this.getParentId()[0]);
			Navigation nav = adminManager.findNavByNavId(navId);
			
			if(nav!=null){
				this.setNav(nav);
				for(int i=0;i<imageIdList.length;i++){
					
					Image image = adminManager.findImageByImageId(imageIdList[i]);
					if(image!=null){
						image.setNav(nav);
						adminManager.modify_image(image);
						Language lang = nav.getLang();
						if(lang!=null){
							this.getImageInfo().setLang(lang);
						}
						
						this.getImageInfo().setImage(image);
						adminManager.add_imageInfo(this.getImageInfo());
					}
					
				}
			}
		}
		
		imageIdList = new int[100];
		count = 0;
		return "image_add_success";
	}
	private static int pageSize=10;
	private int pageNo;
	private long totalCount;
	private int pageCount;
	
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public long getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(long totalCount) {
		this.totalCount = totalCount;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public int getPageSize() {
		return pageSize;
	}
	
	private ImageInfoVo imageInfoVo;
	private List<ImageInfoVo> imageInfoVoList;
	
	public ImageInfoVo getImageInfoVo() {
		return imageInfoVo;
	}
	public void setImageInfoVo(ImageInfoVo imageInfoVo) {
		this.imageInfoVo = imageInfoVo;
	}
	public List<ImageInfoVo> getImageInfoVoList() {
		return imageInfoVoList;
	}
	public void setImageInfoVoList(List<ImageInfoVo> imageInfoVoList) {
		this.imageInfoVoList = imageInfoVoList;
	}
	
	private String paramNavId;
	
	public String getParamNavId() {
		return paramNavId;
	}
	public void setParamNavId(String paramNavId) {
		this.paramNavId = paramNavId;
	}
	public String find_imageList() throws Exception{
		
		if(this.getImageInfoVoList()!=null){
			this.setImageInfoVoList(null);
		}
		if(this.getParamNavId()!=null){
			
			int navId = Integer.parseInt(this.getParamNavId());
			Navigation nav = adminManager.findNavByNavId(navId);
			List<Navigation> navList = new LinkedList<Navigation>();
			if(nav!=null){
				navList.add(nav);
				
				this.setNav(nav);
				List<Navigation> nList = adminManager.findNavListByNavPid(nav.getNavId());
				
				if(nList!=null&&nList.size()>=1){
					
					navList.addAll(nList);
				}
			}
			
			long totalCount = adminManager.find_imageListCountByNavList(navList);
			this.setTotalCount(totalCount);
			int pageCount = (int) (this.getTotalCount()/this.getPageSize()+1);
			this.setPageCount(pageCount);
			if(this.getPageNo()==0){
				this.setPageNo(1);
			}
			if(this.getTotalCount()==0){
				this.setPageNo(0);
				this.setPageCount(0);
			}
			
			if(this.getPageNo()==1){
				List<ImageInfoVo> imageInfoVoList = new LinkedList<ImageInfoVo>();
				int offset = 0;
				List<Image> imageList = adminManager.findImageByNavListAndPage(navList, offset, this.getPageSize());
				if(imageList!=null&&imageList.size()>=1){
					
					for(int i=0;i<imageList.size();i++){
						ImageInfoVo imageInfoVo = new ImageInfoVo();
						Image image = imageList.get(i);
						imageInfoVo.setImageId(image.getImageId());
						imageInfoVo.setImageSavePath(image.getImageSavePath());
						Navigation n = image.getNav();
						if(n!=null){
							imageInfoVo.setNavId(n.getNavId());
							imageInfoVo.setNavTitle(n.getTitle());
						}
						ImageInfo imageInfo = adminManager.findImageInfoByImage(image);
						if(imageInfo!=null){
							imageInfoVo.setImageInfoId(imageInfo.getImageInfoId());
							imageInfoVo.setImageTitle(imageInfo.getTitle());
							imageInfoVo.setImageInfo(imageInfo.getInfo());
						}
						imageInfoVoList.add(imageInfoVo);
					}
					if(imageInfoVoList!=null&&imageInfoVoList.size()>=1){
						this.setImageInfoVoList(imageInfoVoList);
					}
					
				}
			}
			if(this.getPageNo()>1&&this.getPageNo()<=this.getPageCount()){
				List<ImageInfoVo> imageInfoVoList = new LinkedList<ImageInfoVo>();
				int offset = (this.getPageNo()-1)*this.getPageSize();
				List<Image> imageList = adminManager.findImageByNavListAndPage(navList, offset, this.getPageSize());
				if(imageList!=null&&imageList.size()>=1){
					
					for(int i=0;i<imageList.size();i++){
						ImageInfoVo imageInfoVo = new ImageInfoVo();
						Image image = imageList.get(i);
						imageInfoVo.setImageId(image.getImageId());
						imageInfoVo.setImageSavePath(image.getImageSavePath());
						Navigation n = image.getNav();
						if(n!=null){
							imageInfoVo.setNavId(n.getNavId());
							imageInfoVo.setNavTitle(n.getTitle());
						}
						ImageInfo imageInfo = adminManager.findImageInfoByImage(image);
						if(imageInfo!=null){
							imageInfoVo.setImageInfoId(imageInfo.getImageInfoId());
							imageInfoVo.setImageTitle(imageInfo.getTitle());
							imageInfoVo.setImageInfo(imageInfo.getInfo());
						}
						imageInfoVoList.add(imageInfoVo);
					}
					if(imageInfoVoList!=null&&imageInfoVoList.size()>=1){
						this.setImageInfoVoList(imageInfoVoList);
					}
				}
			}
			this.setParamNavId(null);
		}else if(this.getNav()!=null&&this.getNav().getNavId()!=null){
			
			Navigation nav = adminManager.findNavByNavId(this.getNav().getNavId());
			List<Navigation> navList = new LinkedList<Navigation>();
			if(nav!=null){
				navList.add(nav);
				this.setNav(nav);
				List<Navigation> nList = adminManager.findNavListByNavPid(nav.getNavId());
				
				if(nList!=null&&nList.size()>=1){
					
					navList.addAll(nList);
				}
				
			}
			
			long totalCount = adminManager.find_imageListCountByNavList(navList);
			this.setTotalCount(totalCount);
			int pageCount = (int) (this.getTotalCount()/this.getPageSize()+1);
			this.setPageCount(pageCount);
			if(this.getPageNo()==0){
				this.setPageNo(1);
			}
			if(this.getTotalCount()==0){
				this.setPageNo(0);
				this.setPageCount(0);
			}
			
			if(this.getPageNo()==1){
				List<ImageInfoVo> imageInfoVoList = new LinkedList<ImageInfoVo>();
				int offset = 0;
				List<Image> imageList = adminManager.findImageByNavListAndPage(navList, offset, this.getPageSize());
				if(imageList!=null&&imageList.size()>=1){
					
					for(int i=0;i<imageList.size();i++){
						ImageInfoVo imageInfoVo = new ImageInfoVo();
						Image image = imageList.get(i);
						imageInfoVo.setImageId(image.getImageId());
						imageInfoVo.setImageSavePath(image.getImageSavePath());
						Navigation n = image.getNav();
						if(n!=null){
							imageInfoVo.setNavId(n.getNavId());
							imageInfoVo.setNavTitle(n.getTitle());
						}
						ImageInfo imageInfo = adminManager.findImageInfoByImage(image);
						if(imageInfo!=null){
							imageInfoVo.setImageInfoId(imageInfo.getImageInfoId());
							imageInfoVo.setImageTitle(imageInfo.getTitle());
							imageInfoVo.setImageInfo(imageInfo.getInfo());
						}
						imageInfoVoList.add(imageInfoVo);
					}
					if(imageInfoVoList!=null&&imageInfoVoList.size()>=1){
						this.setImageInfoVoList(imageInfoVoList);
					}
					
				}
			}
			if(this.getPageNo()>1&&this.getPageNo()<=this.getPageCount()){
				List<ImageInfoVo> imageInfoVoList = new LinkedList<ImageInfoVo>();
				int offset = (this.getPageNo()-1)*this.getPageSize();
				List<Image> imageList = adminManager.findImageByNavListAndPage(navList, offset, this.getPageSize());
				if(imageList!=null&&imageList.size()>=1){
					
					for(int i=0;i<imageList.size();i++){
						ImageInfoVo imageInfoVo = new ImageInfoVo();
						Image image = imageList.get(i);
						imageInfoVo.setImageId(image.getImageId());
						imageInfoVo.setImageSavePath(image.getImageSavePath());
						Navigation n = image.getNav();
						if(n!=null){
							imageInfoVo.setNavId(n.getNavId());
							imageInfoVo.setNavTitle(n.getTitle());
						}
						ImageInfo imageInfo = adminManager.findImageInfoByImage(image);
						if(imageInfo!=null){
							imageInfoVo.setImageInfoId(imageInfo.getImageInfoId());
							imageInfoVo.setImageTitle(imageInfo.getTitle());
							imageInfoVo.setImageInfo(imageInfo.getInfo());
						}
						imageInfoVoList.add(imageInfoVo);
					}
					if(imageInfoVoList!=null&&imageInfoVoList.size()>=1){
						this.setImageInfoVoList(imageInfoVoList);
					}
				}
			}
			
		}
		
		return "imageList_find_success";
	}
	
	public String del_image() throws Exception{
		
		if(this.getImage()!=null&&this.getImage().getImageId()!=null){
			
			adminManager.del_image(image);
			
		}
		
		
		
		return "image_del_success";
	}
	
	
}
